//
//  HAJSBridgeHandlerEventMethod.m
//  HAFund
//
//  Created by 夏成龙 on 2018/11/12.
//  Copyright © 2018年 夏成龙. All rights reserved.
//

#import "HAJSBridgeHandlerEventMethod.h"
#import "HAJSBridgeHandlerEvent.h"

#import <objc/runtime.h>

@implementation HAJSBridgeHandlerEventMethod

- (void)closeWindow:(HAJSEvent *)event {
    NSLog(@"%s", __func__);
    if (event.callBackName.length > 0) {
        [event.handlerEvent callBackFunctionWithEvnet:event params:@{@"test":@"1"}];
    }
}

- (void)openWindow:(HAJSEvent *)event {
    NSLog(@"%s", __func__);
    if ([event.bridgeHandler respondsToSelector:@selector(bridgeHandlerWithEvent:handler:)]) {
        [event.bridgeHandler bridgeHandlerWithEvent:event handler:^(id params) {
           
            if (event.callBackName.length > 0) {
                [event.handlerEvent callBackFunctionWithEvnet:event params:params];
            }
        }];
    }
}



- (NSArray<HAJSBridgeHandlerEventMethod *> *)subObject {
    if (!_subObject) {
        int count = objc_getClassList(NULL,0);
        if (count <= 0) {
            return [NSArray array];
        }
        
        NSMutableArray *subArray = [NSMutableArray arrayWithObject:self];
        
        Class *classes = (Class *)malloc(sizeof(Class) * count);
        objc_getClassList(classes, count);
        
        for (int i = 0; i < count; i++) {
            if (self.class == class_getSuperclass(classes[i])) {
                NSObject *objc = [[classes[i] alloc] init];
                [subArray addObject: objc];
            }
        }
        free(classes);
        _subObject = subArray;
    }
    return _subObject;
}

@end
